package com.qumitech.visualnovel.service.applepay.impl;

import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.exception.BusinessException;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.controller.vo.applePay.ApplePayVO;
import com.qumitech.visualnovel.model.common.UrlConstant;
import com.qumitech.visualnovel.service.applepay.ApplePayService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class ApplePayServiceImpl implements ApplePayService {

    private final String APPLE_PAY = "/pay/apple/config";
    private final String APPLE_PAY_ADMIN = "/admin/pay/apple/config";


    @Override
    public void saveApplePay(ApplePayVO applePayVO) {
        String url = UrlConstant.SERVER_URL + APPLE_PAY;
        String req = JSONObject.toJSONString(applePayVO);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.BLOCK_APPLICATION_EXIST);
        }
    }

    @Override
    public Map<String, Object> getApplePayList(String paymentName, Integer offset, Integer limit) {
        String url = UrlConstant.SERVER_URL + APPLE_PAY_ADMIN;
        Map<String, String> paramMap = new HashMap<>();
           if (offset != null) {
            paramMap.put("offset", offset.toString());
        }
        if (limit != null) {
            paramMap.put("limit", limit.toString());
        }
        if (paymentName != null) {
            paramMap.put("payment_name", paymentName);
        }

        String body = HttpInvokeUtil.httpGet(url, paramMap, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()
                || rsp.getData() == null) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
        Map<String, Object> rspMap = (Map<String, Object>) rsp.getData();
        return rspMap;
    }

    @Override
    public void updateApplePay(ApplePayVO applePayVO, String paymentId) {
        String url = UrlConstant.SERVER_URL + APPLE_PAY + "/" + paymentId;
        String req = JSONObject.toJSONString(applePayVO);
        String body = HttpInvokeUtil.httpPutJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
    }

    @Override
    public void removeApplyPay(String paymentId) {
        String url = UrlConstant.SERVER_URL + APPLE_PAY + "/" + paymentId;
        Map<String, String> headMap = new HashMap<>();
        String body = HttpInvokeUtil.httpDelete(url, headMap);
        BaseRsp rsp = JSONObject.parseObject(body, BaseRsp.class);
        if (rsp == null || rsp.getStatus() != ResultEnum.SUCCESS.getState()) {
            throw new BusinessException(ResultEnum.FAILURE);
        }
    }

}
